﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MDIApplication.Classes;

namespace MDIApplication
{
    public partial class NewBooking : Form
    {
        public NewBooking()
        {
            InitializeComponent();
        }

        #region Form_Load
        private void NewBooking_Load(object sender, EventArgs e)
        {
            this.dateTimePicker1.MinDate = DateTime.Now;
            this.dateTimePicker1.Value = DateTime.Now;
            this.dateTimePicker2.Value = dateTimePicker1.Value.AddDays(1);
            this.dateTimePicker2.MinDate = dateTimePicker2.Value;
            this.comboBox1.SelectedIndex = 0;
        }
        #endregion Form_Load

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            dateTimePicker2.MinDate = dateTimePicker1.Value.AddDays(1);
        }

        #region buttons
        RoomAvailability ra;
        private void button1_Click(object sender, EventArgs e)
        {

            DataTable availableroomTab = new DataTable();
            SqlConnection connection = new SqlConnection(@"Data Source=LUCKYPX\LUCKYPX;Initial Catalog=SWENAssignment3;Persist Security Info=True;User ID=sa;Password=imsa");
            connection.Open();
            string query;
            if (comboBox1.Text == "All Room")
            {
                query = "Select * from Room where Room_Status='Available'";
            }
            else
            {
                query = "Select * from Room where Room_Status='Available' and Room_Type='" + comboBox1.Text + "'";
            }
            availableroomTab.Load(new SqlCommand(query, connection).ExecuteReader());
            connection.Close();
            if (availableroomTab.Rows.Count == 0)
            {
                MessageBox.Show("There is no available room.");                                          
            }
            else
            { 
                CloseAllForms();
                if (ra == null)
                {
                 ra = new RoomAvailability(dateTimePicker1.Value, dateTimePicker2.Value, comboBox1.Text, availableroomTab);
                 ra.MdiParent = this.MdiParent;
                 ra.Location = new Point(100, 70);
                 ra.Show();
                }              
            }
        
           
        }


        private void button2_Click(object sender, EventArgs e)
        {
            this.dateTimePicker1.MinDate = DateTime.Now;
            this.dateTimePicker1.Value= DateTime.Now;
            this.dateTimePicker2.Value = dateTimePicker1.Value.AddDays(1);
            this.comboBox1.SelectedIndex = 0;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion buttons
 
        #region CloseAllForms
        private void CloseAllForms()
        {
            Form[] formToClose = null;
            int i = 1;
            foreach (Form form in Application.OpenForms)
            {
                if (form != this && form.IsMdiChild) //this is form1
                {
                    Array.Resize(ref formToClose, i);
                    formToClose[i - 1] = form;
                    i++;
                }
            }
            if (formToClose != null)
            {
                for (int j = 0; j < formToClose.Length; j++)
                {
                    formToClose[j].Close();
                }
                ra = null;
            }
        }
        #endregion CloseAllForms

    }
}
